package com.yanxi.controller;

import com.yanxi.pojo.Result;
import com.yanxi.utils.AliyunOSSOperator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class UploadController {
    public static final Logger log = LoggerFactory.getLogger(UploadController.class);
    @Autowired
    public AliyunOSSOperator  aliyunOSSOperator;

    //        本地磁盘存储
/*    @PostMapping("/upload")
    public Result upload(String name, Integer age, MultipartFile file) throws IOException {


        log.info("上传:{},{},{}",  name, age, file);
        //获取文件名
        String originalFilename = file.getOriginalFilename();
        //新的文件名
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString() + extension;
        //保存文件
        file.transferTo(new File("E:/image/"+newFileName));
        return Result.success("https://web-framework.oss-cn-hangzhou.aliyuncs.com/2022-09-03-07-37-38222.jpg");



    }*/


    //阿里云oss存储
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("上传:{}", file.getOriginalFilename());
        //将文件上传到阿里云oss
        String url = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        log.info("文件上传成功:{}", url);
        return Result.success(url);
    }
}
